/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
import cloneDeep from 'lodash/cloneDeep';
import { encodeDate } from 'src/util/format';

const CPU_VALUE = 'CPU';
const NEICUN_VALUE = 'MEMORY';
const GB = 'GB';
const CUNCHU_VALUE = 'PVC';
const GPU_VALUE = 'VGPU';
const SUANLI_VALUE = 'VGPU_RATIO';
const XIANCUN_VALUE = 'VGPU_MEMORY';
const TYPE_OPTIONS_HOME = [
    {
        value: SUANLI_VALUE,
        label: _('算力')
    },
    {
        value: XIANCUN_VALUE,
        label: _('显存')
    }
];

const TYPE_OPTIONS_USER = [
    {
        value: CPU_VALUE,
        label: _('CPU')
    },
    {
        value: NEICUN_VALUE,
        label: _('内存')
    },
    {
        value: GPU_VALUE,
        label: _('GPU')
    },
];

const TYPE_OPTION_GPU = [
    {
        value: SUANLI_VALUE,
        label: _('GPU（算力）')
    },
    {
        value: XIANCUN_VALUE,
        label: _('GPU（显存）')
    }
]

const TIME_WEEK = 'lastSevenDays';
const THREE_DAY = 'lastThreeDays';
const ONE_DAY = 'lastOneDays';
const THREE_HOUR = 'lastThreeHours';
const ONE_HOUR = 'lastOneHours';
const HALF_HOUR = 'lastHalfHours'
const TIME_OPTIONS_HOME = [
    // {value: TIME_WEEK, label: _('近七天')},
    // {value: THREE_DAY, label: _('近三天')},
    {value: ONE_HOUR, label: _('近一小时')},
    {value: THREE_HOUR, label: _('近三小时')},
    {value: ONE_DAY, label: _('近一天')}
];


const TIME_OPTIONS_USER = [
    {value: TIME_WEEK, label: _('近七天')},
    {value: THREE_DAY, label: _('近三天')},
    {value: ONE_DAY, label: _('近一天')},
    {value: THREE_HOUR, label: _('近三小时')},
    {value: ONE_HOUR, label: _('近一小时')}
];

const TIME_OPTIONS_RESOURCE = [
    {value: THREE_HOUR, label: _('三小时')},
    {value: ONE_HOUR, label: _('一小时')},
    {value: HALF_HOUR, label: _('半小时')}
]

const Y_CONFIG = {
    [GB]:{
        name:'GB'
    },
    [CPU_VALUE]: {
        name: '核'
    },
    [NEICUN_VALUE]: {
        name: 'G'
    },
    [CUNCHU_VALUE]: {
        name: 'G'
    },
    [SUANLI_VALUE]: {
        name: '%',
        max: 100,
        min: 0,
        interval:20,
    },
    [XIANCUN_VALUE]: {
        name: '单位：MB',
        min: 0
    }
};

function getYConfig (type, options) {
    let config = Y_CONFIG[type];
    if (type === XIANCUN_VALUE) {
        config.max = Math.ceil(options.yMax || 5);
        config.interval = Math.ceil((options.yMax || 5) / 5)
    }
    return config
}

function getDefaultTypeConfig(type) {
    let date = new Date();
    let config = {
        [TIME_WEEK]: {
            interval: 24 * 60 * 60 * 1000, // 固定x轴时间间隔 间隔24小时，也就是一天
            min: encodeDate(Math.round(cloneDeep(date).setDate(date.getDate() - 7) / 1000)),
            max: encodeDate(Math.round(cloneDeep(date).setDate(date.getDate()) / 1000))
        },
        [THREE_DAY]: {
            interval: 24 * 60 * 60 * 1000,
            min: encodeDate(Math.round(cloneDeep(date).setDate(date.getDate() - 3) / 1000)),
            max: encodeDate(Math.round(cloneDeep(date).setDate(date.getDate()) / 1000))
        },
        [ONE_DAY]: {
            interval: 4 * 60 * 60 * 1000,
            min: encodeDate(Math.round(cloneDeep(date).setHours(date.getHours() - 24) / 1000)),
            max: encodeDate(Math.round(cloneDeep(date).setHours(date.getHours()) / 1000))
        },
        [THREE_HOUR]: {
            interval: 60 * 60 * 1000,
            min: encodeDate(Math.round(cloneDeep(date).setHours(date.getHours() - 3) / 1000)),
            max: encodeDate(Math.round(cloneDeep(date).setHours(date.getHours()) / 1000))
        },
        [ONE_HOUR]: {
            interval: 10 * 60 * 1000,
            min: encodeDate(Math.round(cloneDeep(date).setMinutes(date.getMinutes() - 60) / 1000)),
            max: encodeDate(Math.round(cloneDeep(date).setMinutes(date.getMinutes()) / 1000))
        },
        [HALF_HOUR]: {
            interval: 5 * 60 * 1000,
            min: encodeDate(Math.round(cloneDeep(date).setMinutes(date.getMinutes() - 30) / 1000)),
            max: encodeDate(Math.round(cloneDeep(date).setMinutes(date.getMinutes()) / 1000))
        }
    };
    return config[type];
}

function getDefaultTime (type) {
    let date = new Date();
    let arr = [];

    switch (type) {
        case TIME_WEEK:
            for (let i = 0; i <= 7; i ++) {
                let computedDate = cloneDeep(date).setDate(date.getDate() - i);
                arr.unshift({
                    x: Math.round(computedDate / 1000).toString(),
                    y: 0
                });
            }
            break;

        case THREE_DAY:
            for (let i = 0; i <= 3; i ++) {
                let computedDate = cloneDeep(date).setDate(date.getDate() - i);
                arr.unshift({
                    x: Math.round(computedDate / 1000).toString(),
                    y: 0
                });
            }
            break;

        case ONE_DAY:
            for (let i = 0; i <= 6; i ++) {
                let computedDate = cloneDeep(date).setHours(date.getHours() - i * 4);
                arr.unshift({
                    x: Math.round(computedDate / 1000).toString(),
                    y: 0
                });
            }
            break;

        case THREE_HOUR:
            for (let i = 0; i <= 3; i ++) {
                let computedDate = cloneDeep(date).setHours(date.getHours() - i);
                arr.unshift({
                    x: Math.round(computedDate / 1000).toString(),
                    y: 0
                });
            }
            break;

        case ONE_HOUR:
            for (let i = 0; i <= 6; i ++) {
                let computedDate = cloneDeep(date).setMinutes(date.getMinutes() - i * 10);
                arr.unshift({
                    x: Math.round(computedDate / 1000).toString(),
                    y: 0
                });
            }
            break;

        default:
            break
    }

    return arr;
}

export {
    TYPE_OPTION_GPU,
    SUANLI_VALUE,
    GPU_VALUE,
    XIANCUN_VALUE,
    TYPE_OPTIONS_HOME,
    TYPE_OPTIONS_USER,
    TIME_WEEK,
    ONE_DAY,
    ONE_HOUR,
    HALF_HOUR,
    getYConfig,
    TIME_OPTIONS_HOME,
    TIME_OPTIONS_USER,
    TIME_OPTIONS_RESOURCE,
    getDefaultTypeConfig,
    getDefaultTime
}
